/************************************************************************
 * File: company.cpp                                                    *
 *                                                                      *
 * Copyright 2006, 2008 Colin Kern (colin.kern@gmail.com)               *
 *                                                                      *
 * This file is part of Profit.  Some previous versions were released   *
 * under the names Megacorp Mania and Acquire.                          *
 *                                                                      *
 * Profit is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU General Public License as published by *
 * the Free Software Foundation, either version 3 of the License, or    *
 * any later version.                                                   *
 *                                                                      *
 * Profit is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with Profit.  If not, see <http://www.gnu.org/licenses/>.      *
 ************************************************************************/
#include "company.h"
#include "player.h"
#include <QString>
#include <QColor>
#include <QPainter>
#include <QMouseEvent>

Company::Company(const QString &name, const QColor &color,
	Profit::CostGroup group, QWidget *parent)
	: QWidget(parent)
{
    m_name = name;
    m_group = group;
	m_color = color;
	setNormalColor();
	setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	m_mouseHover = false;
}

const QString &Company::getName() const
{
    return m_name;
}

QChar Company::getInitial() const
{
    return getName().at(0).toUpper();
}

int Company::getSize() const
{
    return m_size;
}

Profit::CostGroup Company::getGroup( ) const
{
    return m_group;
}

int Company::nShares( ) const
{
    return m_shares;
}

int Company::getPricePerShare( ) const
{
    if (getSize() == 0) return 0;
    int modifier;
    if ( getGroup() == Profit::low )
        modifier = 0;
    else if ( getGroup() == Profit::medium )
        modifier = 100;
    else
        modifier = 200;

    if ( getSize() < 6 )
        return ( getSize() * 100 + modifier );
    else if ( getSize() < 11 )
        return ( 600 + modifier );
    else if ( getSize() < 21 )
        return ( 700 + modifier );
    else if ( getSize() < 31 )
        return ( 800 + modifier );
    else if ( getSize() < 41 )
        return ( 900 + modifier );
    else
        return ( 1000 + modifier );
}

int Company::getMajorityBenefits( ) const
{
    return getPricePerShare() * 10;
}

int Company::getMinorityBenefits( ) const
{
    return getMajorityBenefits() / 2;
}

QSize Company::sizeHint() const
{
	return QSize(200, 40);
}

const QColor &Company::getColor() const
{
	return m_color;
}

bool Company::canBuy(Player *player) const
{
	return (getSize() > 0 && nShares() > 0 && player->getBuyLimit() > 0
		&& player->getMoney() >= getPricePerShare());
}

bool Company::inMerge() const
{
	return m_inMerge;
}

void Company::increaseSize(int amount)
{
    m_size += amount;
	update();
	emit sizeChanged(m_size);
}

void Company::setSize(int size)
{
	if (m_size != size) {
		m_size = size;
		update();
		emit sizeChanged(m_size);
	}
}

void Company::giveShares(int amount)
{
    m_shares += amount;
	update();
}

void Company::takeShares(int amount)
{
    m_shares -= amount;
	updateCurrentColor();
	update();
}

void Company::reset(int initialShares)
{
    m_size = 0;
    m_shares = initialShares;   
	update();
}

void Company::updateCurrentColor()
{
	if (m_phase == Profit::FormCompany) {
		if (getSize() > 0) {
			setDimColor();
		} else if (getSize() == 0 && m_mouseHover) {
			setLitColor();
		} else {
			setNormalColor();
		}
	} else if (m_phase == Profit::BuyShares) {
		if (!canBuy(m_turn)) {
			setDimColor();
			setMouseHover(false);
		} else if (canBuy(m_turn) && m_mouseHover) {
			setLitColor();
		} else {
			setNormalColor();
		}
	} else if (m_phase == Profit::MergeCompanies ||
		m_phase == Profit::ChooseMerge) {
		if (m_inMerge && m_mouseHover) {
			setLitColor();
		} else if (!m_inMerge) {
			setDimColor();
		} else {
			setNormalColor();
		}
	} else {
		setDimColor();
	}
}

void Company::setNormalColor()
{
	if (m_state != Normal) {
		m_gradient1 = m_color.darker();
		m_gradient2 = m_color.lighter(125);
		m_state = Normal;
		update();
	}
}

void Company::setLitColor()
{
	if (m_state != Lit) {
		m_gradient1 = m_color;
		m_gradient2 = m_color.lighter();
		m_state = Lit;
		update();
	}
}

void Company::setDimColor()
{
	if (m_state != Dim) {
		m_gradient1 = QColor(64, 64, 64);
		m_gradient2 = m_color.darker();
		m_state = Dim;
		update();
	}
}

void Company::setInMerge()
{
	m_inMerge = true;
	updateCurrentColor();
}

void Company::unsetInMerge()
{
	m_inMerge = false;
	updateCurrentColor();
}

void Company::changePhase(Profit::Phase newPhase)
{
	m_phase = newPhase;
	updateCurrentColor();
	if (m_mouseHover) {
		emit hoverChange(m_mouseHover, this);
	}
}

void Company::setTurn(Player *player)
{
	m_turn = player;
}

void Company::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	
	QLinearGradient gradient(rect().topLeft(), rect().bottomRight());
	gradient.setColorAt(0.0, m_gradient1);
	gradient.setColorAt(1.0, m_gradient2);
	
	painter.setPen(Qt::NoPen);
	painter.setBrush(QBrush(gradient));
	painter.drawRoundedRect(rect(), 10, 10);
	
	painter.setPen(Qt::white);
	painter.setFont(QFont("Helvetica", 12, QFont::Bold));
	
	QRect top(rect().topLeft(), QPoint(rect().right(), rect().center().y()));
	QRect bottom(QPoint(rect().left(), rect().center().y()), rect().bottomRight());
	
	QString line1;
	QString line2;
	
	if (getSize() == 0) {	
		line1 = m_name;
		if (getGroup() == Profit::low) {
			line2 = "low";
		} else if (getGroup() == Profit::medium) {
			line2 = "medium";
		} else if (getGroup() == Profit::high) {
			line2 = "high";
		}
	} else {
		line1 = m_name + " (" + QString::number(getSize()) + ")";
		line2 =	QString::number(nShares()) + ", $" + 
			QString::number(getPricePerShare());
	}
	
	painter.drawText(top, Qt::AlignBottom | Qt::AlignHCenter, line1);
	painter.setFont(QFont("Helvetica", 12));
	painter.drawText(bottom, Qt::AlignTop | Qt::AlignHCenter, line2);
}

void Company::enterEvent(QEvent *event)
{
	setMouseHover(true);
	updateCurrentColor();
}

void Company::leaveEvent(QEvent *event)
{
	setMouseHover(false);
	updateCurrentColor();
}

void Company::mousePressEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton) {
		emit clicked(this);
	}
}

void Company::setMouseHover(bool hover)
{
	if (m_mouseHover != hover) {
		m_mouseHover = hover;
		emit hoverChange(m_mouseHover, this);
	}
}